#!/usr/bin/python3
# -*- coding: UTF-8 -*-

'''Run self tests.'''

# (c) 2008 Alberto Milone <albertomilone@alice.it>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import unittest, os.path, sys, logging, os, atexit
import getopt
import resource

def usage():
    instructionsList = ['The only accepted (optional) parameters are:'
    '\n  -o, --output=<dirname>', '\tthe directory where the results \n\
\t\t\t\tof the tests are saved.'
    
    '\n  -i, --input=<filename>', '\tthe xorg.conf used for the tests.'
    
    '\n  -h, --help', '\t\t\thelp page.'

    '\n  [Test ...]', '\t\t\tList of tests, the entire testsuite will be executed otherwise.'
    ]
    print(''.join(instructionsList))

def main():
    # Set a high limit of open fds
    soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
    if soft < 8180:
        resource.setrlimit(resource.RLIMIT_NOFILE, (8180, hard))
    cwd = os.getcwd()
    inputFile = os.path.join(cwd, 'xorg.conf')
    outputDir = cwd
    err = 'Error: parameters not recognised'
    
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'h:o:i:', ['help', 'output=', 'input='])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err) # will print something like 'option -a not recognized'
        usage()
        sys.exit(2)
    printonly = None
    verbose = None
    for o, a in opts:
        if o in ('-i', '--input'):
            inputFile = a
        elif o in ('-o', '--output'):
            outputDir = a
        elif o in ('-h', '--help'):
            usage()
            sys.exit()
        else:
            assert False, 'unhandled option'
    
    
    settingsFile = open('settings.py', 'w')
    atexit.register(os.unlink, 'settings.py')
    if inputFile == os.path.join(cwd, 'xorg.conf') and outputDir == cwd:
        settingsFile.write('import os\ncwd = os.getcwd()\ninputFile = os.path.join(cwd, "xorg.conf")\noutputDir = cwd\ninputDir = cwd.replace("tests", "quirks")')
    else:    
        settingsFile.write('inputFile = "%s"\noutputDir = "%s"' % (inputFile, outputDir))
    settingsFile.close()
        
    # run all tests in our directory
    if args:
        suite = unittest.TestLoader().loadTestsFromNames(args)
    else:
        suite = unittest.TestLoader().loadTestsFromNames(
            [t[:-3] for t in os.listdir(os.path.dirname(__file__))
            if t.endswith('.py') and t not in ['settings.py', '__init__.py'] and not
            (t == 'gpu-manager.py' and '86' not in os.uname()[4])])
    res = unittest.TextTestRunner(verbosity=2).run(suite)
    return len(res.errors) + len(res.failures)

if __name__ == '__main__':
    # run ourselves through umockdev-wrapper
    if 'umockdev' not in os.environ.get('LD_PRELOAD', ''):
        os.execvp('umockdev-wrapper', ['umockdev-wrapper'] + sys.argv)

    sys.exit(main())
